package com.silverbox.shopper.dubboApi.goods;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.silverbox.shopper.entity.*;
import com.silverbox.shopper.entity.extend.*;
import com.silverbox.shopper.entity.extend.GoodsGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.silverbox.core.annotation.DataSource;
import com.silverbox.core.exception.CoreException;
import com.silverbox.core.exception.CoreException.CoreExceptionCode;
import com.silverbox.core.util.Arith;
import com.silverbox.core.util.Utils;
import com.silverbox.shopper.service.goods.ShopperGoodsService;
import com.silverbox.sweeporder.controller.pay.PayController;

@Service
@DataSource(name = DataSource.DATASOURCE_A)
public class ShopperGoodsServiceApiImpl implements ShopperGoodsServiceApi {

	private static Logger logger = LoggerFactory.getLogger(ShopperGoodsServiceApiImpl.class);
	
	@Autowired
	private ShopperGoodsService shopperGoodsService;

	@Override
	public List<CategoryGoods> getCategoryGoods(GoodsExtendParam param) {
		if(param==null){
			throw new CoreException("GoodsExtendParam is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(param.getShopperId()==null){
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(param.getStoreId()==null){
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		logger.info("查询 params = "+param.toString());
		return shopperGoodsService.getCategoryGoods(param);
	}

	@Override
	public List<DiscountInfo> getDiscountGoods(String shopId, Integer goodsId,String nowDate,Integer dayTime) {
		if (shopId == null) {
			throw new CoreException("shopId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (nowDate == null) {
			throw new CoreException("nowDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getDiscountInfoList(shopId, goodsId, nowDate, dayTime);
	}

    @Override
    public int isEffectGoodsGroup(Integer shopperId, Integer storeId) {
	    if (shopperId==null){
	        throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
	    if (storeId==null){
            throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        return shopperGoodsService.isEffectGoodsGroup(storeId,shopperId);
    }

    @Override
    public List<GoodsGroup> getEffsctGroupByStoreId(Integer storeId, Integer shopperId) {
        if (shopperId==null){
            throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        if (storeId==null){
            throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        return shopperGoodsService.getEffsctGroupByStoreId(storeId, shopperId);
    }

    @Override
    public List<GoodsGroup> getDefaultGroupByStoreId(Integer storeId, Integer shopperId) {
        if (shopperId==null){
            throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        if (storeId==null){
            throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        return shopperGoodsService.getDefaultGroupByStoreId(storeId, shopperId);
    }

    @Override
    public List<GoodsGroup> getDefaultGroup(Integer storeId, Integer shopperId) {
        if (shopperId==null){
            throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        if (storeId==null){
            throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        return shopperGoodsService.getDefaultGroup(storeId, shopperId);
    }

    @Override
    public List<CategoryGoods> getEffsctGoodsGroupByStoreId(GoodsGroupParam goodsGroupParam) {
        if (goodsGroupParam==null){
            throw new CoreException("goodsGroupParam is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        if (goodsGroupParam.getShopperId()==null){
            throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        if (goodsGroupParam.getGroupId()==null){
            throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        if (goodsGroupParam.getGroupId()==null){
            throw new CoreException("groupId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        return shopperGoodsService.getEffsctGoodsGroupByStoreId(goodsGroupParam);
    }


    @Override
    public List<GoodsGroup> getEffsctGroupByShopperId(Integer storeId,Integer shopperId) {
        if (shopperId==null){
            throw new CoreException("shopperId is null",CoreExceptionCode.EXCP_PARAM_NULL);
        }
        if (storeId==null){
            throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        return shopperGoodsService.getEffsctGroupByShopperId(storeId,shopperId);
    }

    @Override
    public List<GoodsGroup> getDefaultEffsctGroupByShopperId(Integer storeId,Integer shopperId) {
        if (shopperId==null){
            throw new CoreException("shopperId is null",CoreExceptionCode.EXCP_PARAM_NULL);
        }
        if (storeId==null){
            throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        return shopperGoodsService.getDefaultEffsctGroupByShopperId(storeId,shopperId);
    }

    @Override
    public List<CategoryGoods> getEffsctGoodsGroupByShopperId(GoodsGroupParam goodsGroupParam) {
        if (goodsGroupParam==null){
            throw new CoreException("goodsGroupParam is null",CoreExceptionCode.EXCP_PARAM_NULL);
        }
	    if (goodsGroupParam.getShopperId()==null){
	        throw new CoreException("shopperId is null",CoreExceptionCode.EXCP_PARAM_NULL);
        }
        if (goodsGroupParam.getGroupId()==null){
            throw new CoreException("groupId is null",CoreExceptionCode.EXCP_PARAM_NULL);
        }
        return shopperGoodsService.getEffsctGoodsGroupByShopperId(goodsGroupParam);
    }



   /* @Override
    public List<CategoryGoods> getGoodsDefaultSize(GoodsExtendParam param) {
	    if (param==null){
	        throw new CoreException("param is mull",CoreExceptionCode.EXCP_PARAM_NULL);
        }
	    if (param.getStoreId()==null){
	        throw new CoreException("StoreId is null",CoreExceptionCode.EXCP_PARAM_NULL);
        }
	    if (param.getShopperId()==null){
	        throw new CoreException("shoppId is null",CoreExceptionCode.EXCP_PARAM_NULL);
        }
        return shopperGoodsService.getGoodsDefaultSize(param);
    }*/

    @Override
	public List<CombinationPackage> getCombinationPackageList(Integer shopperId, Integer storeId) {
		if (shopperId == null) {
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (storeId == null) {
			throw new CoreException("shopId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getCombinationPackageList(shopperId, storeId);
	}

	@Override
	public List<NormalPackage> getNormalPackageList(Integer shopperId) {
		if (shopperId == null) {
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getNormalPackageList(shopperId);
	}

	@Override
	public List<GoodsPropsExtend> getGoodsBindPropsList(Integer goodsId, Integer shopperId, Integer storeId) {
		if (goodsId == null) {
			throw new CoreException("goodsId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (shopperId == null) {
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (storeId == null) {
			throw new CoreException("shopId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getGoodsBindPropsList(goodsId, shopperId, storeId);
	}

	@Override
	public List<GoodsPropsExtend> getShopBindPropsList(Integer shopperId, Integer storeId) {
		if (shopperId == null) {
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (storeId == null) {
			throw new CoreException("shopId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getShopBindPropsList(shopperId, storeId);
	}

	@Override
	public List<GoodsPropsExtend> getAllGoodsBindPropsList(Integer shopperId, Integer storeId) {
		if (shopperId == null) {
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (storeId == null) {
			throw new CoreException("shopId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getAllGoodsBindPropsList(shopperId, storeId);
	}

	@Override
	public List<DaypartingGoods> getDaypartingGoods(Integer storeId, Integer weekNum, String nowDate) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (weekNum == null) {
			throw new CoreException("weekNum is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (nowDate == null) {
			throw new CoreException("nowDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getDaypartingGoods(storeId, weekNum, nowDate);
	}

	@Override
	public List<Map<String,Object>> isSelloutByGoodsId(Integer storeId, List<Integer> goodsId, String startDate, String endDate,
			String nowDate) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (CollectionUtils.isEmpty(goodsId)) {
			throw new CoreException("goodsId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (startDate == null) {
			throw new CoreException("startDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (endDate == null) {
			throw new CoreException("endDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (nowDate == null) {
			throw new CoreException("nowDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		} else {
			return shopperGoodsService.isSelloutByGoodsId(storeId, goodsId, startDate, endDate, nowDate);
		}
	}

	@Override
	public List<CategoryGoods> searchGoodsByGoodsName(GoodsExtendParam param) {
		if (param.getGoodsName() == null) {
			throw new CoreException("goodsName is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getCategoryGoods(param);
	}

    @Override
    public List<CategoryGoodsDetail> getGoodsDetail(Integer storeId, Integer goodsId) {
	    if (storeId==null){
            throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
	    if (goodsId==null){
            throw new CoreException("goodsId is null", CoreExceptionCode.EXCP_PARAM_NULL);
        }
        return shopperGoodsService.getGoodsDetail(storeId, goodsId);
    }

    @Override
	public List<Size> getSizeInfoByGoodsId(List<Integer> goodsIds) {
		if (CollectionUtils.isEmpty(goodsIds)) {
			throw new CoreException("goodsIds is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getSizeInfoByGoodsId(goodsIds);
	}

	@Override
	public List<CombinationPackage> getCombinationPackage(Integer shopperId, Integer storeId, String startDate,
			String endDate, String nowDate, Integer packageId) {
		if (shopperId == null) {
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (startDate == null) {
			throw new CoreException("startDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (endDate == null) {
			throw new CoreException("endDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (nowDate == null) {
			throw new CoreException("nowDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (packageId == null) {
			throw new CoreException("packageId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getCombinationPackage(shopperId, storeId, startDate, endDate, nowDate, packageId);
	}

	@Override
	public List<NormalPackage> getNormalPackage(Integer shopperId, Integer storeId, Integer packageId) {
		if (shopperId == null) {
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (packageId == null) {
			throw new CoreException("packageId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		
		return shopperGoodsService.getNormalPackage(shopperId, storeId, packageId);
	}

	@Override
	public void updateHotByCallAble(Integer storeId,Integer shopperPid, String nowDate, String weekDate) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (StringUtils.isEmpty(nowDate)) {
			throw new CoreException("nowDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (StringUtils.isEmpty(weekDate)) {
			throw new CoreException("weekDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
//		shopperGoodsService.updateHotByCallAble(storeId, nowDate, weekDate);
		//查询自定义本周最火商品
		List<HotGoodsWeek> hotGoodsCustomWeekList = shopperGoodsService.getCustomHotGoodsWeek(storeId, shopperPid);
		if(!Utils.isEmpty(hotGoodsCustomWeekList)){
			for(HotGoodsWeek goodsWeek : hotGoodsCustomWeekList){
				Integer goodsId = goodsWeek.getGoodsId();
				Long sizeId = goodsWeek.getSizeId();
				Double salesVolume = goodsWeek.getSalesVolume() == null ? 0d : goodsWeek.getSalesVolume();
				//查询自定义本周最火商品
				List<HotGoodsWeek> hotGoodsCustomList = shopperGoodsService.selectHotGoodsSqlForCustomSet(storeId, shopperPid, nowDate, weekDate, goodsId, sizeId);
				//如果自定义本周最火商品不为空，则对其进行修改
				if(Utils.isEmpty(hotGoodsCustomList) || Utils.isEmpty(hotGoodsCustomList.get(0)) || hotGoodsCustomList.get(0) == null){ 
					continue;
				}
				Double volume = hotGoodsCustomList.get(0).getVolume();
				if(volume!=null) {
					Double totalVolume = Arith.add(salesVolume, volume);
					shopperGoodsService.updateHotGoodsSqlForCustomSet(totalVolume, storeId, shopperPid, goodsId, sizeId);	
				}
			}
		}
		
		//清空系统本周最火商品表
		shopperGoodsService.deleteHotGoodsSqlForSystemSet(storeId, shopperPid);
		//重新查询统计系统本周最火商品
		List<HotGoodsWeek> hotGoodsSystemList = shopperGoodsService.selectHotGoodsSqlForSystemSet(storeId, shopperPid, nowDate, weekDate);
		//如果系统本周最火商品及销量不为空则进行插入
		if(!Utils.isEmpty(hotGoodsSystemList)){
			List<HotGoodsWeek> newHotGoodsSystemList = new ArrayList<HotGoodsWeek>();
			Date createTime = new Date();
			for(HotGoodsWeek hot : hotGoodsSystemList){
				hot.setCreateTime(new Date());
				newHotGoodsSystemList.add(hot);
			}
			shopperGoodsService.insertHotGoodsSqlForSystemSet(createTime, newHotGoodsSystemList);	
		}
	}

	@Override
	public List<HotGoodsWeek> getHotWeekGoods(Integer storeId, Integer isHand, Integer maxNum) {
		if (storeId == null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (isHand == null) {
			throw new CoreException("isHand is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (maxNum == null) {
			throw new CoreException("maxNum is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getHotWeekGoods(storeId, isHand, maxNum);
	}

	@Override
	public List<CategoryGoods> getSystemGoodsInfo(List<String> systemGoods) {
		if (CollectionUtils.isEmpty(systemGoods)) {
			throw new CoreException("systemGoods is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getSystemGoodsInfo(systemGoods);
	}

	@Override
	public List<HotGoodsWeek> getOldHotGoodsWeek(Integer storeId, String todayStart, String todayEnd) {
		return shopperGoodsService.getOldHotGoodsWeek(storeId, todayStart, todayEnd);
	}

	@Override
	public List<CategoryGoods> getNeedGoodsInfoList(GoodsExtendParam goodsExtendParam) {
		if (goodsExtendParam==null) {
			throw new CoreException("goodsExtendParam is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (goodsExtendParam.getStoreId()==null) {
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (goodsExtendParam.getShopperId()==null) {
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (StringUtils.isEmpty(goodsExtendParam.getStartDate())) {
			throw new CoreException("startDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (StringUtils.isEmpty(goodsExtendParam.getEndDate())) {
			throw new CoreException("endDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (StringUtils.isEmpty(goodsExtendParam.getNowDate())) {
			throw new CoreException("nowDate is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getNeedGoodsInfoList(goodsExtendParam);
	}

	@Override
	public Qrcode getQrcodeGoodsInfo(Integer storeId, Integer goodsId, Long sizeId) {
		if(StringUtils.isEmpty(goodsId)){
			throw new CoreException("goodsUID is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(StringUtils.isEmpty(storeId)){
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getQrcodeGoodsInfo(storeId,goodsId,sizeId);
	}

	@Override
	public List<CategoryGoods> getGoodsInfoByGoodsId(GoodsExtendParam goodsExtendParam) {
		if(goodsExtendParam==null){
			throw new CoreException("goodsExtendParam is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(goodsExtendParam.getGoodsId()==null){
			throw new CoreException("goodsId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(goodsExtendParam.getStoreId()==null){
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getGoodsInfoByGoodsId(goodsExtendParam);
	}

	@Override
	public List<RequiredCategory> getRequiredCategory(Integer storeId, Integer shopperId, String tableUid) {
		if(storeId==null){
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(shopperId==null){
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getRequiredCategory(storeId,shopperId,tableUid);
	}

	@Override
	public List<SystemGoodsExtend> getShopSystemGoods(Integer storeId, Integer shopperId, String tableUid,int weekNum) {
		if(storeId==null){
			throw new CoreException("storeId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(shopperId==null){
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(weekNum>=8 || weekNum<=0){
			throw new CoreException("weekNum 为1到7", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getShopSystemGoods(storeId,shopperId,tableUid,weekNum);
	}

	@Override
	public List<GoodsPropsExtend> getGoodsCookingsList(Integer goodsId, Integer shopperId) {
		if (goodsId == null) {
			throw new CoreException("goodsId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if (shopperId == null) {
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		
		return shopperGoodsService.getGoodsCookingsList(goodsId, shopperId);
	}

	@Override
	public List<GoodsPropsExtend> getAllGoodsCookingsList(Integer shopperId) {
		if (shopperId == null) {
			throw new CoreException("shopperId is null", CoreExceptionCode.EXCP_PARAM_NULL);
		}
		
		return shopperGoodsService.getAllGoodsCookingsList(shopperId);
	}

	@Override
	public List<RecommendGoods> getRecommendGoods(Integer shopperId, Integer storeId) {
		if(shopperId == null){
			throw new CoreException("shopperId is null",CoreExceptionCode.EXCP_PARAM_NULL);
		}
		if(storeId == null){
			throw new CoreException("storeId is null",CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getRecommendGoods(shopperId,storeId);
	}

	@Override
	public List<GroupGoodsExtend> getGroupGoodsList(Integer cookId) {
		if(cookId == null){
			throw new CoreException("cookId is null",CoreExceptionCode.EXCP_PARAM_NULL);
		}
		return shopperGoodsService.getGroupGoodsList(cookId);
	}
}
